<template>
  <div :class="className" :style="{ height: height, width: width, minWidth: width }" />
</template>

<script>

// import resize from './mixins/resize'

import * as echarts from 'echarts';
require("echarts/theme/macarons");

export default {
  // mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'echarts-echartCard'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '320px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    sName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions({ xData, sData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: xData,
          boundaryGap: false,
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            color: '#9DA6AE'
          }
        },
        grid: {
          left: 10,
          right: 24,
          bottom: 20,
          top: 15,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          renderMode: 'html',
          backgroundColor: '#F4F6FB',
          formatter(params) {
            const [firstElement] = params
            return `<div class="echartCard">
            <p class="echartCard-title">${firstElement.axisValueLabel}</p>
            <div class="echartCard-panel"><span>${firstElement.seriesName}</span><span class="echartCard-value">${(
                Number(firstElement.value)
              ).toLocaleString()}</span></div>
          </div>`
          },
          className: 'echarts-tooltip echarts-tooltip-dark'
        },
        yAxis: {
          minInterval: 1,
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          axisLabel: {
            color: '#9DA6AE'
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              color: '#E5E8EF'
            }
          },
          splitArea: {
            show: true
          }
        },
        series: [{
          name: this.sName,
          lineStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
              {
                offset: 0,
                color: '#1EE7FF'
              },
              {
                offset: 1,
                color: '#6F42FB'
              }
            ])
          },
          areaStyle: {
            opacity: 0.8,
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: '#DBECFF'
              },
              {
                offset: 1,
                color: '#FDFEFF'
              }
            ])
          },
          showSymbol: false,
          smooth: true,
          type: 'line',
          data: sData,
          animationDuration: 2800,
          animationEasing: 'cubicInOut'
        }
        ]
      }, true)
    }
  }
}
</script>
